import {
  PersonModel
} from '../../models/hdd/personal.js'
let personModel = new PersonModel()
import {
  LoginModel
} from '../../models/hdd/login.js'
let loginModel = new LoginModel()
import {
  config
} from '../../config.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    FName: '',
    FScale: '',
    FFoundingTime: '',
    FRegistCapital: '',
    FManager: '',
    FAdress: '',
    FDoorNumber: '',
    FMobile: '',
    FIntroduce: '',
    FId: '',
    userId: '',
    mobile: '',
    scales: ['10人以下', '10-20人', '20-50人', '50人以上'],
    scaleIdx: 0,
  },

  scalesSelector(e) {
    this.setData({
      scaleIdx: e.detail.value,
    })
  },

  dateSelector(e) {
    this.setData({
      FFoundingTime: e.detail.value
    })
  },

  statusSelector(e) {
    this.setData({
      statusIndex: e.detail.value
    })
  },

  nameInput(e) {
    this.setData({
      FName: e.detail.value
    })
  },

  capitalInput(e) {
    this.setData({
      FRegistCapital: e.detail.value
    })
  },

  managerInput(e) {
    this.setData({
      FManager: e.detail.value
    })
  },

  adressInput(e) {
    this.setData({
      FAdress: e.detail.value
    })
  },

  doorNumberInput(e) {
    this.setData({
      FDoorNumber: e.detail.value
    })
  },

  mobileInput(e) {
    this.setData({
      FMobile: e.detail.value
    })
  },

  introduceInput(e) {
    this.setData({
      FIntroduce: e.detail.value
    })
  },

  selectAddress() {
    let that = this
    wx.getSetting({
      success(res) {
        if (!res.authSetting['scope.userLocation']) {
          wx.authorize({
            scope: 'scope.userLocation',
            success() {
              wx.chooseLocation({
                success: function (res) {
                  console.log(res)
                  that.setData({
                    FAdress: res.name
                  })
                },
              })
            },
            fail() {
              wx.showModal({
                showCancel: false,
                title: '提示',
                content: '请打开位置开关',
                success(res) {
                  if (res.confirm) {
                    wx.openSetting({
                      success(res) {
                        if (res.authSetting['scope.userLocation']) {
                          wx.chooseLocation({
                            success: function (res) {
                              that.setData({
                                FAdress: res.name
                              })
                            },
                          })
                        }
                      }
                    })
                  }
                }
              })
            }
          })
        } else {
          wx.chooseLocation({
            success: function (res) {
              console.log(res)
              that.setData({
                FAdress: res.name
              })
            }
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    wx.setNavigationBarTitle({
      title: '公司资料完善'
    })
    wx.setNavigationBarColor({
      frontColor: '#ffffff',
      backgroundColor: '#FF7136',
      animation: {
        duration: 400,
        timingFunc: 'easeIn'
      }
    })
    if (options.FId) {
      this.setData({
        FId: options.FId
      })
      this.requestCompanyInfo()
    } else {
      var date = new Date()
      var year = date.getFullYear()
      var month = date.getMonth() + 1
      var day = date.getDate()
      if (month < 10) {
        month = "0" + month;
      }
      if (day < 10) {
        day = "0" + day;
      }
      this.setData({
        FFoundingTime: year + '-' + month + '-' + day,
        mobile: options.mobile,
        userId: options.userId
      })
    }
  },

  requestCompanyInfo() {
    personModel.getPersonalData({
      FId: this.data.FId
    }).then(res => {
      this.setData({
        FName: res.data.data.data.company.FName,
        FScale: res.data.data.data.company.FScale,
        FFoundingTime: res.data.data.data.company.FFoundingTime,
        FRegistCapital: res.data.data.data.company.FRegistCapital,
        FManager: res.data.data.data.company.FManager,
        FAdress: res.data.data.data.company.FAdress,
        FDoorNumber: res.data.data.data.company.FDoorNumber,
        FMobile: res.data.data.data.company.FMobile,
        FIntroduce: res.data.data.data.company.FIntroduce,
      })
    })
  },

  checkData() {
    if (!this.data.FName) {
      wx.showModal({
        showCancel: false,
        title: '提示',
        content: '请填写公司名称'
      })
      return
    }
    if (!this.data.FManager) {
      wx.showModal({
        showCancel: false,
        title: '提示',
        content: '请填写法人代表'
      })
      return
    }
    if (!this.data.FAdress) {
      wx.showModal({
        showCancel: false,
        title: '提示',
        content: '请填写公司地址'
      })
      return
    }
    if (!this.data.FMobile) {
      wx.showModal({
        showCancel: false,
        title: '提示',
        content: '请填写联系电话'
      })
      return
    }
    this.register()
  },

  submitPic() {
    var that = this
    wx.uploadFile({
      url: `${config.api_base_url}api/Ecp.Picture.upload.jup`,
      filePath: that.data.photo,
      name: 'file',
      header: {
        "Content-Type": "multipart/form-data",
        'Cookie': 'MEETREND_CRM_TOKEN=' + wx.getStorageSync('token')
      },
      formData: {
        'user': 'test'
      },
      success: function(res) {
        that.setData({
          'info.FPhoto': JSON.parse(res.data).data.pictureId
        })
        that.saveDataWithPic()
      },
      fail: function(e) {
        wx.showModal({
          title: '提示',
          content: '上传失败',
          showCancel: false
        })
      },
      complete: function() {}
    })
  },

  saveDataWithPic() {
    personModel.savePersonalData({
      args: {
        data: [this.data.info]
      }
    }).then(res => {
      wx.hideLoading()
      wx.navigateTo({
        url: '/pages/workExperience/workExperience',
      })
    })
  },

  register() {
    let param
    if(this.data.FId) {
      param = {
        FName: this.data.FName,
        FScale: this.data.scales[this.data.scaleIdx],
        FFoundingTime: this.data.FFoundingTime,
        FRegistCapital: this.data.FRegistCapital,
        FManager: this.data.FManager,
        FAdress: this.data.FAdress,
        FDoorNumber: this.data.FDoorNumber,
        FMobile: this.data.FMobile,
        FIntroduce: this.data.FIntroduce,
        FId: this.data.FId
      }
    } else {
      param = {
        FName: this.data.FName,
        FScale: this.data.scales[this.data.scaleIdx],
        FFoundingTime: this.data.FFoundingTime,
        FRegistCapital: this.data.FRegistCapital,
        FManager: this.data.FManager,
        FAdress: this.data.FAdress,
        FDoorNumber: this.data.FDoorNumber,
        FMobile: this.data.FMobile,
        FIntroduce: this.data.FIntroduce
      }
    }
    personModel.saveMerchantInfo({
      args: {
        data: [param]
      }
    }).then(res => {
      if (res.data.success) {
        wx.navigateTo({
          url: `/pages/hdd-auth/hdd-auth?mobile=${this.data.mobile}&companyId=${res.data.data.entityIds[0]}`,
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})